#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi
import wsgiref.handlers
import os
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.db import GqlQuery
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.api import memcache
from db_weather import DbWeather
from db_weather_yahoo import DbWeatherYahoo
from db_city import DbCity

class Tianqi(webapp.RequestHandler):
  def get(self):
    db_weather_query = GqlQuery("SELECT * FROM DbWeather order by date desc")
    template_values = {'weatherdbs': db_weather_query}
    path = os.path.join(os.path.dirname(__file__), 'tianqi.html')
    self.response.out.write(template.render(path, template_values))

class TianqiYahooCity(webapp.RequestHandler):
  def get(self):
    woeid = self.request.get('woeid')
    name = self.request.get('name')
    weatherdbs = DbWeatherYahoo.all().order('-date').filter('woeid =', woeid)
    template_values = {'weatherdbs': weatherdbs, 'name': name}
    #path = os.path.join(os.path.dirname(__file__), 'tianqiYahooCityData.html')
    path = os.path.join(os.path.dirname(__file__), 'tianqiYahooCity.html')
    self.response.out.write(template.render(path, template_values))
    #self.response.out.write(woeid + "!!!")

class TianqiYahoo(webapp.RequestHandler):
  def get(self):
    #'''
    hangzhou = GqlQuery("SELECT * FROM DbCity where name = 'Hangzhou'").get()
    hangzhou_high = DbWeatherYahoo.all().order('-date').filter('woeid =', hangzhou.woeid_weather).get()
    same_high = DbWeatherYahoo.all().order('-date').filter('high =', hangzhou_high.high)
    
    citys_all = memcache.get("citys_cache")
    if citys_all is not None:
      pass
    else:
      citys_all = DbCity.all()
      memcache.add("citys_cache", citys_all)

    citys_geodict = memcache.get("citys_woeid_geo_cache")
    if citys_geodict is not None:
      pass
    else:
      citys_geodict = {}
      for city in citys_all:
        citys_geodict[city.woeid_weather] = city.geo
      memcache.add("citys_woeid_geo_cache", citys_geodict)
    
    geos = []
    for weather in same_high:
      geo = citys_geodict[weather.woeid]
      geos.append(geo)

    template_values = {'weathers': same_high,
                       'geos': geos}
    
    path = os.path.join(os.path.dirname(__file__), 'tianqiYahoo2.xml')
    self.response.out.write(template.render(path, template_values))
    #'''